﻿using System;
using System.Data;
using System.Collections.Generic;

using VIT.Web.UI;
using VIT.Web.Controls;
using VIT.Provider;
using VIT.Library;
using VIT.Library.Web;

namespace VIT.Web.Admin
{
    public partial class FaqGroupEdit : VIT.Web.UI.VITComponent
    {
        public string genOption = string.Empty;
        VIT.Provider.FaqGroup obj; 

        protected void Page_Load(object sender, EventArgs e)
        {
            obj = new VIT.Provider.FaqGroup();
            try
            {
                obj.Id = int.Parse(Request["sId"]);
            }
            catch
            {
                obj.Id = -1;
            }

            if (obj.Id < 0) ButtonInsert.Visible = true; else ButtonModify.Visible = true;

            if (!IsPostBack)
            {
                LoadData();
                LoadLanguages();
            } 
        }

        #region Methods
        private void LoadData()
        {
            obj.LanguageId = Session["language"].ToString();
            obj.GetSingle();
            if (obj.Id > 0)
            {
                txtTitle.Text = obj.Title;
                txtDes.Text = obj.Description;
                txtOrder.Text = obj.Orders.ToString();
                chkPublish.Checked = obj.Published;
                if (!string.IsNullOrEmpty(obj.Image))
                {
                    img.Visible = true;
                    img.ImageUrl = URLRoot + "Uploads/Images/Faqs/" + obj.Image;
                    img.AlternateText = obj.Image;
                }
            }
            LoadParent();
        }

        private void LoadLanguages()
        {
            Language language = new Language();
            ddlLang.DataSource = language.getAll();
            ddlLang.DataTextField = "Name";
            ddlLang.DataValueField = "Id";
            ddlLang.DataBind();

            ddlLang.SelectedValue = Session["language"].ToString();
        }

        private void LoadParent()
        {
            DataTable dt = obj.GetAll();
            this.genOption = dt.GenTagOption("Id", "Title", "0", obj.ParentId.ToString());
        }

        private void save()
        {
            // bat loi tren server
            var data = new Dictionary<dynamic, List<object>> {
                                                        {txtTitle, new List<object> {"required"}},
                                                        {txtOrder, new List<object> {"number"}}
                                                    };
            if (MainCore.checkData(data))
            {
                bool imgvalid = true;
                if (Request["parentid"] == obj.Id.ToString()) setTrueVisible(lblNhomCha);
                else
                {
                    if (fupImg.HasFile)
                    {
                        if (MainCore.checkFile(fupImg.FileName, "isImage"))
                        {
                            obj.Image = String.Format("{0:yyMMddhhmmss}", DateTime.Now) + System.IO.Path.GetExtension(fupImg.FileName);
                            fupImg.SaveAs(URLBaseDir + "Uploads/Images/Faqs/" + obj.Image);
                        }
                        else
                        {
                            imgvalid = false;
                            setTrueVisible(lblHinhKoDung);
                        }
                    }
                    else obj.Image = img.AlternateText;

                    if (imgvalid)
                    {
                        obj.ParentId = int.Parse(Request["parentid"]);
                        obj.LanguageId = ddlLang.SelectedValue;
                        obj.Title = txtTitle.Text;
                        obj.Description = txtDes.Text;
                        obj.Orders = int.Parse(txtOrder.Text);
                        obj.Published = chkPublish.Checked;

                        if (obj.Save() > 0) setTrueVisible(lblThanhCong);
                        else setTrueVisible(lblThatBai);
                    }
                }
            }
            else setTrueVisible(lblThatBai);
        }

        private void setTrueVisible(System.Web.UI.HtmlControls.HtmlGenericControl control)
        {
            lblNhomCha.Visible = false;
            lblThanhCong.Visible = false;
            lblThatBai.Visible = false;

            control.Visible = true;
        }
        #endregion

        #region Events
        protected void Save_Click(object sender, EventArgs e)
        {
            save();
            RedirectComponent("Exchange", "FaqGroupList");
        }

        protected void Apply_Click(object sender, EventArgs e)
        {
            save();
            LoadData();
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            RedirectComponent("Exchange", "FaqGroupList");
        }

        protected void ddlLang_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["language"] = ddlLang.SelectedValue;
            LoadData();
        }
        #endregion
    }
}
